/*
 * Copyright (c) 2011 by Konstantin Pavlov.
 */
package gwtlx.html5.client;

import gwtlx.html5.client.cache.ApplicationCache;
import gwtlx.html5.client.system.SystemInfo;

/**
 * Browser is utility class for checking browser features.
 *
 * @author Konstantin Pavlov
 * @version $LastChangedRevision: 19 $
 * @since 29.05.2010
 */
public abstract class Browser {

    /**
     * Checks if browser supports Geolocaiton API.
     *
     * @return true if Geolocaiton is supported
     *
     * @see <a href="http://diveintohtml5.org/detect.html#geolocation">Geolocation API Detection</a>
     */
    public static native boolean hasGeolocationSupport() /*-{
        return !!$wnd.navigator.geolocation;
    }-*/;

    public static native boolean hasSystemInfoSupport() /*-{
        return !!$wnd.navigator.system;
    }-*/;

    public static native boolean isSessionStorageSupported() /*-{
        try {
            return (!$wnd.sessionStorage != null);
        } catch (e) {
            return false;
        }
    }-*/;

    /**
     * Checks if browser supports WebWorkers.
     * <p/>
     * Uses technoque described <a href="http://diveintohtml5.org/detect.html#workers">here</a>.
     *
     * @return true if browser supports web workers
     */
    public static native boolean hasWebWorkerSupport() /*-{
        return !!$wnd.Worker;
    }-*/;

    public static native Storage getSessionStorage() /*-{
        return @gwtlx.html5.client.Storage::as(Lcom/google/gwt/core/client/JavaScriptObject;)($wnd.sessionStorage);
    }-*/;

    public static native boolean hasLocalStorageSupport() /*-{
        return ($wnd.localStorage != null);
    }-*/;

    /**
     * Returns <code>true<code> if browser has support for <code>placeholder</code> attribute of form elements.
     *
     * @return true if <code>placeholder</code> attribute is supported
     */
    public static native boolean hasPlaceholderSupport() /*-{
        return ('placeholder' in document.createElement('input'));
    }-*/;


    public static native Storage getLocalStorage() /*-{
        return @gwtlx.html5.client.Storage::as(Lcom/google/gwt/core/client/JavaScriptObject;)(localStorage);
    }-*/;

    public static native SystemInfo getSystemInfo() /*-{
        return ($wnd.navigator.system);
    }-*/;

    /**
     * Returns true if applicationCache object is supported by browser.
     *
     * @return true if applicationCache object is supported by browser.
     */
    public static native boolean hasAppCacheSupport() /*-{
        return ($wnd.applicationCache != null);
    }-*/;

    /** @return applicationCache or null, if not supported by browser */
    public static native ApplicationCache getApplicationCache() /*-{
        return @gwtlx.html5.client.cache.ApplicationCache::as(Lcom/google/gwt/core/client/JavaScriptObject;)($wnd.applicationCache);
    }-*/;


    /**
     * Checks if Microdata API is supported by the browser
     *
     * @return true is Microdata API is not supported by browser
     *
     * @see <a href="http://diveintohtml5.org/detect.html#microdata">Detecting Microdata</a>
     */
    public static native boolean hasMicrodataSupport() /*-{
        return !!$doc.getItems;
    }-*/;


}
